﻿using AutoMapper;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Write.API.Applicantion.Command.SupplierCommand;
using MediatR;

namespace B.S.Traceability.Write.API.Applicantion.CommandHandler.SupplierCommandHandler
{
    public class AddSupplierCommandHandler : IRequestHandler<AddSupplierCommand, APIResult<int>>
    {
        private readonly IBaseRepository<SU_Supplier> suppRep;
        private readonly IMapper mapper;
        public AddSupplierCommandHandler(IBaseRepository<SU_Supplier> suppRep, IMapper mapper)
        {
            this.suppRep = suppRep;
            this.mapper = mapper;
        }
        public Task<APIResult<int>> Handle(AddSupplierCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var model=mapper.Map<SU_Supplier>(request);
            model.SupType=request.SupType;
            model.ProductionCode=request.ProductionCode;
            var res = suppRep.Add(model).Result;
            if (res > 0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
                result.data = res;
            }

            return Task.FromResult(result);
        }
    }
}
